package com.hydra.base.utils;

import javax.servlet.http.HttpServletRequest;

/**
 * 用户IP工具
 * @Author 陈煜敏
 * @Date 2022/9/14 13:45
 */
public class IpUtil {

    /**
     * 从request中获取ip
     */
    public static String getIp(HttpServletRequest request){
        String ip = null;
        String[] headers = new String[]{"x-forwarded-for", "Proxy-Client-IP", "WL-Proxy-Client-IP"};
        for (String header : headers) {
            if(ip == null || "".equals(ip) || "unknown".equalsIgnoreCase(ip)){
                ip = request.getHeader(header);
            }
        }
        if(ip == null || "".equals(ip) || "unknown".equalsIgnoreCase(ip)){
            ip = request.getRemoteAddr();
        }
        return ip;
    }

}
